#ifndef __QyConsoleApplication__
#define __QyConsoleApplication__

#include <QObject>
#include <QProcess>
#include <QStringList>

class QyConsoleApplication : public QObject {
	Q_OBJECT
	Q_PROPERTY( QStringList environment READ environment WRITE setEnvironment )
	Q_PROPERTY( QString workingDirectory READ workingDirectory WRITE setWorkingDirectory )
	Q_PROPERTY( QString applicationName READ applicationName WRITE setApplicationName )
	Q_PROPERTY( QStringList applicationArguments READ applicationArguments WRITE setApplicationArguments )
public:
	explicit QyConsoleApplication( QObject * parent=0 );
	virtual ~QyConsoleApplication();

	QStringList environment() const;
	QString workingDirectory() const;
	QString applicationName() const;
	QStringList applicationArguments() const;
	bool isRunning() const;
	QProcess * process() const { return p_process; }
	QString applicationExePath( const QString & appName );

	static QString envValue( const QString & name, const QStringList & env=QStringList() );
	static QStringList systemPath( const QStringList & env=QStringList() );
	static QStringList normalizeEnvironment( const QStringList & env );
	static void setCurrentEnvironmentValue( const QString & name, const QString & value );
	static void setCurrentEnvironment( const QStringList & environment );

public slots:
	void setEnvironment( const QStringList & env );
	void setWorkingDirectory( const QString & dirPath );
	void setApplicationName( const QString & appName );
	void setApplicationArguments( const QStringList & appArgs );
	void start( const QString & appName, const QStringList & appArgs=QStringList() );
	void run();
	void stop();
	void kill();
	void writeLine( const QString & str );

signals:
	void started();
	void finished( int exitCode );
	void readRequest( const QString & prompt );
	void standartErrorMessage( const QString & text );
	void standartOutputMessage( const QString & text );
	void errorMessage( const QString & text );

protected:
	virtual void processStarted();
	virtual void processFinished( int exitCode );
	virtual void processReadRequest( const QString & prompt );
	virtual void processStandartErrorMessage( const QString & text );
	virtual void processStandartOutputMessage( const QString & text );
	virtual void processErrorMessage( const QString & text );

private slots: //for auto connect
	void on_process_error( QProcess::ProcessError error );
	void on_process_finished( int exitCode, QProcess::ExitStatus exitStatus );
	void on_process_readyReadStandardError();
	void on_process_readyReadStandardOutput();
	void on_process_started();
	void on_process_stateChanged( QProcess::ProcessState newState );
	void on_process_aboutToClose();
	void on_process_bytesWritten( qint64 bytes );
	void on_process_readyRead();

private:
	QProcess * p_process;
	QString p_appName,
		p_lastStdOutMessage;
	QStringList p_appArgs;
	static bool envChanged;

};

#endif/*__QyConsoleApplication__*/
